const app = getApp();

let methods = {
  getActivtyAndCoupont, //查看优惠券
  joinInActivtyAndCoupont, //领取优惠券
  dialog, //弹框
  loginAward, //登录有礼
  awardList, //领取优惠券列表
};

var component = {
  properties: {

  },
  data: {
    xhx_images: app.xhx_images,
    activityTypeId: 1, //优惠券类型
  },
  ready,
  pageLifetimes: {
    show,
  },
  methods: Object.assign({}, methods)
};

Component(component);

function ready() {
  const that = this;

}

function show() {
  const that = this;
  // 异步获取用户信息
  app.xlogin.getUser(function(res) {
    if (res) return that.getActivtyAndCoupont();
    that.setData({
      showAward: true
    })
  })
}

function getActivtyAndCoupont() {
  const that = this;
  const activityTypeId = that.data.activityTypeId;
  app.middleware.requestAjax({
    serAddr: app.serAddrs.getActivtyAndCoupont,
    data: {
      activityTypeId,
    },
    cancelToast: true,
    success: function(res) {
      let lists = res.data || [];
      let showAward = lists.length > 0 ? true : false;
      that.setData({
        showAward,
        lists,
      })
      if (showAward && !app.firstShowDialog) {
        that.dialog();
        app.firstShowDialog = true;
      }
    },
    fail: function() {
      let showAward = false;
      that.setData({
        showAward,
      })
    }
  })
}

function joinInActivtyAndCoupont() {
  const that = this;
  const lists = that.data.lists || [];
  const activityUuid = lists[0].activityUuid;
  const activityTypeId = that.data.activityTypeId;
  app.middleware.requestAjax({
    serAddr: app.serAddrs.joinInActivtyAndCoupont,
    data: {
      activityTypeId,
      activityUuid,
    },
    success: function(res) {
      that.awardList();
      that.setData({
        showAward: false
      })
    }
  })
}

function loginAward() {
  const that = this;
  var myInfo = app.isNavigateToLogin();
  if (!myInfo) return;
  that.dialog();
}

function dialog() {
  const that = this;

  var dialogValue = {
    dialogShow: true,
    dialogType: 1, //领红包弹框
    bgImage: that.data.xhx_images.getCoupons, //背景大图片
    bgWidth: '350px', //背景图宽度
    bgHeight: '410px', //背景图高度
    btnImage: that.data.xhx_images.getBtn, //按钮图片
    btnWidth: '94px', //按钮图宽度
    btnHeight: '94px', //背景图高度
    apply: '仅适用于智能团课', //适用范围
    confirm: function() { //点击按钮的回调
      that.joinInActivtyAndCoupont();
    },
    cancel: function() { //点击×关闭弹框的回调

    }
  }

  that.setData({
    dialogValue
  })
}

function awardList() {
  const that = this;
  const lists = that.data.lists || [];
  lists.forEach(item => {
    item.title = item.name;
    item.validay = item.validEndDate && item.validEndDate.split(" ")[0];
  })
  var dialogValue = {
    dialogShow: true,
    dialogType: 2, //领取、兑换成功弹框
    title: '',
    bgImage: that.data.xhx_images.exchangeBg,
    bgWidth: '700rpx',
    bgHeight: '820rpx',
    btnImage: that.data.xhx_images.exchangeBtn,
    btnWidth: '320rpx',
    btnHeight: '96rpx',
    couponsList: lists,
    apply: '仅适用于智能团课',
    check: '在我的优惠券中查看',
    confirm: function() {
      app.routePush(app.xhx_route.leagueList,"switchTab")
    },
    cancel: function() {

    }
  }
  that.setData({
    dialogValue
  })
}